package com.llxy.worker.service.impl;

import com.llxy.common.core.domain.entity.SysDept;
import com.llxy.common.core.domain.entity.SysDictData;
import com.llxy.common.utils.SecurityUtils;
import com.llxy.system.mapper.SysDeptMapper;
import com.llxy.system.mapper.SysDictDataMapper;
import com.llxy.worker.domain.School;
import com.llxy.worker.domain.SupportWorker;
import com.llxy.worker.dto.TrainDTO;
import com.llxy.worker.mapper.SchoolMapper;
import com.llxy.worker.mapper.SupportWorkerMapper;
import com.llxy.worker.service.SupportWorkerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.llxy.worker.domain.Train;
import com.llxy.worker.mapper.TrainMapper;
import com.llxy.worker.service.TrainService;

import java.util.ArrayList;
import java.util.List;

@Service
public class TrainServiceImpl implements TrainService{

    @Resource
    private TrainMapper trainMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SupportWorkerMapper supportWorkerMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return trainMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Train record) {
        return trainMapper.insert(record);
    }

    @Override
    public int insertSelective(Train record) {
        int count1 = trainMapper.insertSelective(record);
        SupportWorker supportWorker = new SupportWorker();
        supportWorker.setStatus(1);
        supportWorker.setTrainId(record.getId());
        supportWorker.setId(record.getSupportId());
        int count2 = supportWorkerMapper.updateByPrimaryKeySelective(supportWorker);
        return count1 + count2;
    }



    @Override
    public int updateByPrimaryKeySelective(Train record) {
        return trainMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Train record) {
        return trainMapper.updateByPrimaryKey(record);
    }

    @Override
    public TrainDTO selectByPrimaryKey(Integer id) {
        return trainMapper.selectDetail(id);
//        TrainDTO trainDTO = new TrainDTO();
//        Train train = trainMapper.selectByPrimaryKey(id);
//        //培训专业字典
//        List<SysDictData> sysSupportWorkerTypes = sysDictDataMapper.selectDictDataByType("sys_support_worker");
//        //培训专业信息
//        sysSupportWorkerTypes.forEach(type -> {
//            if (train.getSpecialityId().equals(type.getDictValue())){
//                trainDTO.setSpecialityName(type.getDictLabel());
//            }
//        });
//        BeanUtils.copyProperties(trainDTO,train);
//        //根据学校id查询学校信息
//        School school = schoolMapper.selectByPrimaryKey(train.getSchoolId());
//        //根据部门id查询所属部门信息
//        SysDept sysDept = sysDeptMapper.selectDeptById(train.getDeptId());
//        trainDTO.setSysDept(sysDept);
//        trainDTO.setSchool(school);
//        return trainDTO;
    }

    @Override
    public List<TrainDTO> selectTrainList(Long userId,Train train) {
        if (SecurityUtils.isAdmin(userId)) {
            return trainMapper.selectTrainList(null,train);
        }
        return trainMapper.selectTrainList(userId,train);
    }

    @Override
    public Integer selectTrainListCount(Train train) {
        return trainMapper.selectTrainListCount(train);
    }

}
